package cn.com.pc.content.jpa;

import cn.com.pc.content.domain.content.ContentType;

import javax.persistence.AttributeConverter;
import java.util.HashMap;
import java.util.Map;

public class ContentTypeConverter implements AttributeConverter<ContentType, String> {

    private final Map<String, ContentType> map;

    public ContentTypeConverter() {
        map = new HashMap<>();
        for (ContentType t : ContentType.values()) {
            map.put(t.name(), t);
        }
    }

    @Override
    public String convertToDatabaseColumn(ContentType attribute) {
        return attribute == null ? null : attribute.name();
    }

    @Override
    public ContentType convertToEntityAttribute(String dbData) {
        return map.getOrDefault(dbData, ContentType.Other);
    }
}
